<?php
	session_start();
	mysql_connect("localhost", "sapmenet_admin", "a1s2d3f4g5h6j7") or die ("Problem with datebase");
	mysql_select_db("sapmenet_users");
	$imaa=mysql_query ("SELECT publisherID FROM publications WHERE id={$_POST['pubID']}");
	$picID=$_POST['pubID'];
	if (mysql_num_rows($imaa)>0)
	{
	$imaa=mysql_result($imaa, 0);
	$k=0;
	$fr=mysql_query("SELECT confirmed FROM friendships WHERE userID={$_SESSION['JID']} AND friendID={$_SESSION['id']}");
		if (mysql_num_rows($fr)<1)
		{
			if ($_SESSION['JID']!=$_SESSION['id'])
			{
				echo "fr";
			}
			else{
				$who=mysql_query("SELECT whoLikes FROM publications WHERE id={$_POST['pubID']}");
				if (mysql_num_rows($who)>0)
				{
					$who=mysql_result($who,0);
					$wh=explode(",",$who);
					foreach ($wh AS $chovek)
					{
						if ($chovek==$_SESSION['JID']) $k=1;
					}
					if($k==1)
					{
						echo "Haresano";
					}
					else
					{
						
							$lk=mysql_query("SELECT numberOfLikes FROM publications WHERE id={$_POST['pubID']}");
							$lk=mysql_result($lk,0);
							$lk++;
							mysql_query("UPDATE publications SET numberOfLikes={$lk} WHERE id={$_POST['pubID']}");
							$who=$who.",".$_SESSION['JID'];
							mysql_query ("UPDATE publications SET whoLikes='".$who."' WHERE id={$_POST['pubID']}");
							if ($imaa!=$_SESSION['JID'])
							{
							$name=mysql_query("SELECT concat (FirstName, ' ', LastName) AS fullName FROM users WHERE id={$_SESSION['JID']}");
							$name=mysql_result($name, 0);
							$text="<a href=http://sapme.net/Framework/profilePage.php?id=".$_SESSION['JID'].">".$name."</a><span id=".$picID." class=publicationn> liked publication.</span>";
							mysql_query("INSERT INTO notifications (IDpub, text, userID) VALUES ({$_POST['pubID']}, '".$text."', {$imaa})");							
							}
					}
					
				}
				else
				{
					$fr=mysql_result($fr,0);
					if ($fr)
					{
						$lk=mysql_query("SELECT numberOfLikes FROM publications WHERE id={$_POST['pubID']}");
						$lk=mysql_result($lk,0);
						$lk++;
						mysql_query("UPDATE publications SET numberOfLikes={$lk} WHERE id={$_POST['pubID']}");
						mysql_query("UPDATE publications SET whoLikes={$_SESSION['JID']} WHERE id={$_POST['pubID']}");
						$who=$who.",".$_SESSION['JID'];
						mysql_query ("UPDATE publications SET whoLikes='".$who."' WHERE id={$_POST['pubID']}");
						if ($imaa!=$_SESSION['JID'])
						{
						$name=mysql_query("SELECT concat (FirstName, ' ', LastName) AS fullName FROM users WHERE id={$_SESSION['JID']}");
						$name=mysql_result($name, 0);
						$text="<a href=http://sapme.net/Framework/profilePage.php?id=".$_SESSION['JID'].">".$name."</a><span id=".$picID." class=publicationn> liked publication.</span>";
						mysql_query("INSERT INTO notifications (IDpub, text, userID) VALUES ({$_POST['pubID']}, '".$text."', {$imaa})");		
						}
					}
					else
					{
						echo "fr";
					}
					
				}
			}
			}
			else
			{
				$who=mysql_query("SELECT whoLikes FROM publications WHERE id={$_POST['pubID']}");
				if (mysql_num_rows($who)>0)
				{
					$who=mysql_result($who,0);
					$wh=explode(",",$who);
					foreach ($wh AS $chovek)
					{
						if ($chovek==$_SESSION['JID']) $k=1;
					}
					if($k==1)
					{
						echo "Haresano";
					}
					else
					{
						$fr=mysql_result($fr,0);
						if ($fr)
						{
							$lk=mysql_query("SELECT numberOfLikes FROM publications WHERE id={$_POST['pubID']}");
							$lk=mysql_result($lk,0);
							$lk++;
							mysql_query("UPDATE publications SET numberOfLikes={$lk} WHERE id={$_POST['pubID']}");
							$who=$who.",".$_SESSION['JID'];
							mysql_query ("UPDATE publications SET whoLikes='".$who."' WHERE id={$_POST['pubID']}");
							if ($imaa!=$_SESSION['JID'])
							{
							$name=mysql_query("SELECT concat (FirstName, ' ', LastName) AS fullName FROM users WHERE id={$_SESSION['JID']}");
							$name=mysql_result($name, 0);
							$text="<a href=http://sapme.net/Framework/profilePage.php?id=".$_SESSION['JID'].">".$name."</a><span id=".$picID." class=publicationn> liked publication.</span>";
							mysql_query("INSERT INTO notifications (IDpub, text, userID) VALUES ({$_POST['pubID']}, '".$text."', {$imaa})");									
							}
						}
						else
						{
							echo "fr";
						}
					}
					
				}
				else
				{
					$fr=mysql_result($fr,0);
					if ($fr)
					{
						$lk=mysql_query("SELECT numberOfLikes FROM publications WHERE id={$_POST['pubID']}");
						$lk=mysql_result($lk,0);
						$lk++;
						mysql_query("UPDATE publications SET numberOfLikes={$lk} WHERE id={$_POST['pubID']}");
						mysql_query("UPDATE publications SET whoLikes={$_SESSION['JID']} WHERE id={$_POST['pubID']}");
						$who=$who.",".$_SESSION['JID'];
						mysql_query ("UPDATE publications SET whoLikes='".$who."' WHERE id={$_POST['pubID']}");
						if ($imaa!=$_SESSION['JID'])
						{
						$name=mysql_query("SELECT concat (FirstName, ' ', LastName) AS fullName FROM users WHERE id={$_SESSION['JID']}");
						$name=mysql_result($name, 0);
						$text="<a href=http://sapme.net/Framework/profilePage.php?id=".$_SESSION['JID'].">".$name."</a><span id=".$picID." class=publicationn> liked publication.</span>";
						mysql_query("INSERT INTO notifications (IDpub, text, userID) VALUES ({$_POST['pubID']}, '".$text."', {$imaa})");		
						}
					}
					else
					{
						echo "fr";
					}
					
				}
			}
		}
		else
		{
			echo "napsuvai";
		}
					
			
?>
